package comgame;

import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import comgame.scenes.DataSaveManager;
import comgame.scenes.MenuScene;
import javafx.scene.Scene;
import javafx.stage.Stage;
import com.almasb.fxgl.dsl.FXGL;
import factory.PlayerFactory;
import factory.EnemyFactory;
import factory.Fishfactory;

public class Main extends GameApplication {
    @Override
    protected void initSettings(GameSettings settings) {
        settings.setWidth(900);
        settings.setHeight(700);
        settings.setTitle("农场物语");
        settings.setVersion("1.0");
        settings.setAppIcon("icon.png");
    }

    @Override
    protected void initGame() {
        // 只注册 EntityFactory，不生成任何实体
        FXGL.getGameWorld().addEntityFactory(new factory.PlayerFactory());
        FXGL.getGameWorld().addEntityFactory(new factory.EnemyFactory());
        FXGL.getGameWorld().addEntityFactory(new factory.Fishfactory());

        // 全局定时器：每7秒给玩家加鱼塘收益
        FXGL.getGameTimer().runAtInterval(() -> {
            if (comgame.scenes.GameScene.player != null) {
                int income = comgame.scenes.FishpoolScene.fishCount * 1
                        + comgame.scenes.FishpoolScene.greenFishCount * 8
                        + comgame.scenes.FishpoolScene.grayFishCount * 30
                        + comgame.scenes.FishpoolScene.brownFishCount * 80
                        + comgame.scenes.FishpoolScene.orangeFishCount * 15
                        + comgame.scenes.FishpoolScene.pinkFishCount * 20
                        + comgame.scenes.FishpoolScene.redKingFishCount * 100
                        + comgame.scenes.FishpoolScene.liangziFishCount * 15
                        + comgame.scenes.FishpoolScene.caveSharkCount * 500
                        + comgame.scenes.FishpoolScene.arcticShrimpCount * 1;
                comgame.scenes.GameScene.player.addMoney(income);
                
                // 在加钱后自动保存数据，确保数据同步
                comgame.scenes.DataSaveManager.saveAll();
            }
        }, javafx.util.Duration.seconds(7));


        // 只读档，不生成player实体
        comgame.scenes.DataSaveManager.loadAll();

        // 注册关闭钩子，自动存档
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            comgame.scenes.DataSaveManager.saveAll();
        }));
    }

    @Override
    protected void initUI() {
        // 加载主菜单UI
        MenuScene menuScene = new comgame.scenes.MenuScene();
        FXGL.getGameScene().addUINode(menuScene.getRoot());
    }

    public static void main(String[] args) {
        launch(args);
    }
} 